package com.netcracker.educenter.filestorage.servlets.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {

    public static void copy(InputStream inputStream, OutputStream outputStream, byte[] buffer) throws IOException {

        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    }

    public static String rewriteFileNameToLatin(String oldName) {

        StringBuilder newName = new StringBuilder();
        String str;

        for (char ch : oldName.toCharArray()) {

            switch (ch) {
                case 'а' : str = "a"; break;
                case 'б' : str = "b"; break;
                case 'в' : str = "v"; break;
                case 'г' : str = "g"; break;
                case 'д' : str = "d"; break;
                case 'е' : str = "e"; break;
                case 'ё' : str = "yo"; break;
                case 'ж' : str = "zh"; break;
                case 'з' : str = "z"; break;
                case 'и' : str = "i"; break;
                case 'й' : str = "y"; break;
                case 'к' : str = "k"; break;
                case 'л' : str = "l"; break;
                case 'м' : str = "m"; break;
                case 'н' : str = "n"; break;
                case 'о' : str = "o"; break;
                case 'п' : str = "p"; break;
                case 'р' : str = "r"; break;
                case 'с' : str = "s"; break;
                case 'т' : str = "t"; break;
                case 'у' : str = "u"; break;
                case 'ф' : str = "f"; break;
                case 'х' : str = "kh"; break;
                case 'ц' : str = "ts"; break;
                case 'ч' : str = "ch"; break;
                case 'ш' : str = "sh"; break;
                case 'щ' : str = "sch"; break;
                case 'ь' : str = ""; break;
                case 'ы' : str = "y"; break;
                case 'ъ' : str = ""; break;
                case 'э' : str = "e"; break;
                case 'ю' : str = "yu"; break;
                case 'я' : str = "ya"; break;
                case ' ' : str = "_"; break;
                case 'А' : str = "A"; break;
                case 'Б' : str = "B"; break;
                case 'В' : str = "V"; break;
                case 'Г' : str = "G"; break;
                case 'Д' : str = "D"; break;
                case 'Е' : str = "E"; break;
                case 'Ё' : str = "Yo"; break;
                case 'Ж' : str = "Zh"; break;
                case 'З' : str = "Z"; break;
                case 'И' : str = "I"; break;
                case 'Й' : str = "Y"; break;
                case 'К' : str = "K"; break;
                case 'Л' : str = "L"; break;
                case 'М' : str = "M"; break;
                case 'Н' : str = "N"; break;
                case 'О' : str = "O"; break;
                case 'П' : str = "P"; break;
                case 'Р' : str = "R"; break;
                case 'С' : str = "S"; break;
                case 'Т' : str = "T"; break;
                case 'У' : str = "U"; break;
                case 'Ф' : str = "F"; break;
                case 'Х' : str = "Kh"; break;
                case 'Ц' : str = "Ts"; break;
                case 'Ч' : str = "Ch"; break;
                case 'Ш' : str = "Sh"; break;
                case 'Щ' : str = "Sch"; break;
                case 'Э' : str = "E"; break;
                case 'Ю' : str = "Yu"; break;
                case 'Я' : str = "Ya"; break;
                default: {
                    newName.append(ch);
                    continue;
                }
            }

            newName.append(str);
        }

        return newName.toString();
    }
}
